using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using TVT.DataAccess.Repositories;
using System.Web.Security;
using System.Transactions;
using TVT.Common.Exceptions;
using TVT.Common.Helpers;
using MathLib;

namespace TVT.BizLayer.Entities
{
    [Serializable]
    public class UserProfile : BaseEntity<UserProfile>
    {
        #region attribute

        private Guid userId;
        private int high;
        private int weight;
        private bool male;
        private Enumerators.Likes likes;
        private DateTime birthDate;
        private decimal eigenFaceDistance;
        private String eigenVector = String.Empty;
        private Vector eigenFaceVector;

        private static DBRepository repository = DBRepository.GetInstance();

        #endregion
        
        #region properties

        public Guid UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        public int High
        {
            get { return high; }
            set { high = value; }
        }

        public bool Male
        {
            get { return male; }
            set { male = value; }
        }

        public int Weight
        {
            get { return weight; }
            set { weight = value; }
        }

        public Enumerators.Likes Likes
        {
            get { return likes; }
            set { likes = value; }
        }

        public DateTime BirthDate
        {
            get { return birthDate; }
            set { birthDate = value; }
        }

        public decimal EigenFaceDistance
        {
            get { return eigenFaceDistance; }
            set { eigenFaceDistance = value; }
        }

        public String EigenVector
        {
            get { return eigenVector; }
            set { eigenVector = value; }
        }

        public Vector EigenFaceVector
        {
            get 
            {
                if (eigenFaceVector != null)
                    return eigenFaceVector;
                else 
                {
                    eigenFaceVector = Vector.FromString(this.eigenVector);
                }
                return eigenFaceVector;
            }
            set { eigenFaceVector = value; }
        }

        #endregion

        #region Methods

        public override void Insert()
        {
            repository.SaveUserProfile(this.userId, this.high, this.weight, this.male, (int)this.likes, this.birthDate, this.eigenFaceDistance, this.eigenVector);
        }

        public override void Update()
        {
            if (this.eigenFaceVector != null && this.eigenVector == String.Empty)
                this.eigenVector = this.eigenFaceVector.ToString();
            repository.UpdateUserProfile(this.userId, this.high, this.weight, this.male, (int)this.likes, this.birthDate, this.eigenFaceDistance, this.eigenVector);
        }

        public override void Delete()
        {
            throw new NotImplementedException();
        }

        public override void Save()
	    {
            if (this.eigenFaceVector != null && this.eigenVector == String.Empty)
                this.eigenVector = this.eigenFaceVector.ToString();
            this.Insert();
	    }

        public static UserProfile Find(Guid userId)
        {
            return MapToEntity(repository.FindUserProfile(userId));
        }

        #endregion

    }
}
